<template>
	<dt-main-body v-model="labelPosition">
		<div class="location">
			<el-breadcrumb separator-class="el-icon-arrow-right">
				<el-breadcrumb-item>
					<i class="iconfont icon-home"></i>
					管理中心
				</el-breadcrumb-item>
				<el-breadcrumb-item>会员设置</el-breadcrumb-item>
				<el-breadcrumb-item>编辑模板</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		
		<div class="content-box">
			<el-tabs v-model="activeName" type="card">
				<el-tab-pane label="基本信息" name="info">
					<div class="tab-content">
						<el-form ref="editForm" :model="model" :rules="rules" :label-position="labelPosition" label-width="120px">
							<el-form-item prop="type" label="模板类型">
								<el-radio-group v-model="model.type">
								    <el-radio-button label="1">邮件</el-radio-button>
								    <el-radio-button label="2">短信</el-radio-button>
								    <el-radio-button label="3">微信</el-radio-button>
								</el-radio-group>
							</el-form-item>
							<el-form-item prop="callIndex" label="调用名称">
								<el-input v-model="model.callIndex" placeholder="必填，只允许英文数字下划线"></el-input>
							</el-form-item>
							<el-form-item prop="title" label="模板标题">
								<el-input v-model="model.title" placeholder="必填，128字符内"></el-input>
							</el-form-item>
							<el-form-item prop="content" label="模板内容">
								<el-input type="textarea" :rows="5" v-model="model.content" placeholder="必填，支持HTML"></el-input>
							</el-form-item>
							<el-form-item label="更新时间" v-if="model.updateTime">
								{{model.updateTime}}
							</el-form-item>
						</el-form>
					</div>
				</el-tab-pane>
			</el-tabs>
		</div>
		
		<div class="footer-box">
			<div class="footer-btn">
				<el-button type="primary" @click="submitForm">确认保存</el-button>
				<el-button plain @click="$common.back()">返回上一页</el-button>
			</div>
		</div>
	</dt-main-body>
</template>

<script>
	export default {
		props: {
			id: 0
		},
		data() {
			return {
				activeName: 'info', //当前选项卡
				labelPosition: 'right', //表单对齐方式
				model: {
					id: 0,
					type: null,
					callIndex: null,
					title: null,
					content: null,
					updateTime: null,
				},
				rules: {
					type: [
						{ required: true, message: '请选择模板类型', trigger: 'change' }
					],
					callIndex: [
						{ required: true, message: '请输入模板调用名称', trigger: 'blur' },
						{ pattern: /^[a-zA-Z0-9_]*$/, message: '只能是字母数字下划线', trigger: 'blur' }
					],
					title: [
						{ required: true, message: '请输入模板标题', trigger: 'blur' }
					],
					content: [
						{ required: true, message: '请输入模板内容', trigger: 'blur' }
					]
				}
			}
		},
		methods: {
			//初始化数据
			initData() {
				let _this = this; //当前页面
				if (_this.id) {
					_this.$api.request({
						url: `/admin/notify/template/${_this.id}`,
						loading: true,
						success(res) {
							//赋值给model
							Object.keys(_this.model).forEach(key => { _this.model[key] = res.data[key] });
						}
					});
				}
			},
			//提交表单
			submitForm() {
				let _this = this; //当前页面
				//多个表单验证
				const f1 = new Promise((resolve, reject) => {
					_this.$refs['editForm'].validate((valid) => {
						if (valid) resolve()
					});
				});
				Promise.all([f1]).then(() => {
					//提交表单,Id大于0则修改，否则添加
					if (_this.model.id > 0) {
						_this.$api.request({
							method: 'put',
							url: '/admin/notify/template/' + _this.model.id,
							data: _this.model,
							loading: true,
							successMsg: '消息模板修改成功',
							success(res) {
								//可跳转加列表页
								_this.$common.linkUrl('/member/template/list');
							}
						});
					} else {
						_this.$api.request({
							method: 'post',
							url: '/admin/notify/template',
							data: _this.model,
							loading: true,
							successMsg: '消息模板添加成功',
							success(res) {
								//重置表单
								_this.$refs['editForm'].resetFields();
							}
						});
					}
				})
			},
		},
		created() {
			this.initData();
		}
	}
</script>